package com.algorithm.Search;

/**
 * @program: 顺序查找
 * @author: luhd
 * @create: 2021-08-15
 */
public class SequentialSearch {
    public static void main(String[] args) {
        int [] arr = {15,5,3,7,9,5,2,18,10,20,6};
        sequentialSearch(arr,10);
    }
    /*public static int[] sequentialSearch(int[] arr,int k){
        int[] result = new int[arr.length];
        int m = 0;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] == k) {
                result[m++] = arr[i];
            }
        }
        for (int i = 0; i < m; i++) {
            System.out.println(result[i]);
        }
        return result;
    }*/
    // 改进
    public static int sequentialSearch(int[] arr,int k){
        int len = arr.length-1;
        while(len >= 0){
            if(arr[len] == k){
                System.out.println(arr[len]);
                return len;
            }
            len--;
        }
        return 0;
    }
}
